<?php
defined('BASEPATH') or die('Acceso Restringido');

class Tree_Menu
{
	var $_CI;
	var $_forest;
	var $_settings;
	var $_types;

	function Tree_Menu()
	{
		$this->_CI =& get_instance();
		$this->_CI->load->library('treem', 'tree');
		$this->_CI->load->database();

		$this->_forest = array();
		$this->_settings = array();

		$this->_CI->config->load('tree_menu', TRUE, TRUE);
		$config = $this->_CI->config->item('tree_menu');
        $this->_types = $this->_CI->db->get('types')->result_array();
		$this->initialize($config);
	}

	function initialize($config = array())
	{
		if(empty($config))
		{
			$config['pk'] = 'id';
			$config['fk'] = 'parent_id';
			$config['text_column'] = 'nombre';
			$config['toggler_open_class'] = 'toggler_open';
			$config['submenu_class'] = 'sub_menu';
			$config['menu_list_class'] = 'menu_esp';
		}
		$this->_settings = $config;
	}

	function _get_id(&$tree)
	{
		$stdClassObject = $tree->root();

		$vars = get_object_vars($stdClassObject);

		if(array_key_exists($this->_settings['pk'], $vars))
			return $vars[$this->_settings['pk']];

		return 0;
	}
	
	function _get_param($index, &$tree) {
    	
        $stdClassObject = $tree->root();
        $vars = get_object_vars($stdClassObject);
        
        if(array_key_exists($index, $vars))
        	return $vars[$index];
        
        return 0;
        
    }

	function _get_text(&$tree)
	{
		$stdClassObject = $tree->root();

		$vars = get_object_vars($stdClassObject);

		if(array_key_exists($this->_settings['text_column'], $vars))
		{
			$text = $vars[$this->_settings['text_column']];
			return htmlspecialchars($text);
		}
		return '';
	}

	function _get_roots()
	{
		$this->_CI->db->from($this->_settings['table']);
		$this->_CI->db->where($this->_settings['fk'].' = 1');
		$this->_CI->db->where('active', 1);
		$query = $this->_CI->db->get();
		return $query->result();
	}

	function _get_children($id)
	{
		$this->_CI->db->from($this->_settings['table']);
		$this->_CI->db->where($this->_settings['fk'].' IS NOT NULL');
		$this->_CI->db->where($this->_settings['fk'], $id);
		$this->_CI->db->where('active', 1);
		$query = $this->_CI->db->get();

		return $query->result();
	}

	function _has_children($id)
	{
		$query = $this->_CI->db->query(
					"SELECT COUNT(*) AS total FROM "
					.$this->_CI->db->protect_identifiers($this->_settings['table'])
					." WHERE `active` = 1"
					." AND ".$this->_CI->db->protect_identifiers($this->_settings['fk'])					
					." IS NOT NULL "					
					." AND ".$this->_CI->db->protect_identifiers($this->_settings['fk'])
					." = ".$this->_CI->db->escape($id)					
				 );
		$result = $query->row();

		return $result->total > 0;
	}

	function _build_tree(&$tree)
	{        
        
        foreach($tree as &$item) {		  
          $type_id = $this->_get_param('type_id', $item);
          $item->root()->type_text = $this->_types[$type_id - 1];
        }                
		$id = $this->_get_id($tree);

		if($this->_has_children($id))
		{
			$children = $this->_get_children($id);

			foreach($children as $child)
			{
				$child_tree = TreeM::factory($child);

				$this->_build_tree($child_tree);

				$tree->insert($child_tree);
			}
		}
	}

	function _print_sublist(&$tree)
	{
		$id = $this->_get_id($tree);
		$text = $this->_get_text($tree);
        $obj = $this->_get_param('type_text', $tree);
        $type = $obj['typename'];		
		$link = $link = anchor($type.'/view/'.$id, $text);

		if(!$tree->has_children())
			echo '<li>', $link, '</li>';
		else{
		    $link1 = anchor($type.'/view/'.$id, $text);
			echo '<li class="', $this->_settings['toggler_open_class'], '">', $link1;
			echo '<ul class="', $this->_settings['submenu_class'], '">';
			$children =& $tree->children();			
			foreach($children as &$child)			 
			{                
				$this->_print_sublist($child);
			}
			echo '</ul>', '</li>';
		}
	}

	function &build()
	{
		$roots = $this->_get_roots();

		foreach($roots as $root)
		{
			$tree = TreeM::factory($root);
			$this->_build_tree($tree);
			$this->_forest[] = $tree;
		}

		return $this->_forest;
	}

	function print_menu()
	{
		ob_start();
		echo '<ul class="', $this->_settings['menu_list_class'], '">';
		foreach($this->_forest as $tree)
		{
			$this->_print_sublist($tree);
		}
		echo '</ul>';
		$html = ob_get_contents();
		ob_end_clean();
		return $html;
	}
}
